﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TarreOA.TarreOAModels;

namespace TarreOA.TarreOADAL {
    public class Flow_NewFlowStep_DAO {

        /// <summary>
        /// 根据指定条件返回流程步骤信息
        /// </summary>
        /// <param name="sql">要执行的sql语句</param>
        /// <param name="sqlParas">执行语句传入的参数键值对集合</param>
        /// <returns></returns>
        public SqlDataReader FindTestFlowStepInfo(string sql, Hashtable sqlParas, SqlConnection sqlConn)
        {
            SqlCommand sqlCmd = null;
            SqlDataReader sqlDr = null;
            try
            {
                if (sqlConn.State != ConnectionState.Open)
                {
                    sqlConn.Open();
                }
                sqlCmd = new SqlCommand(sql, sqlConn);
                foreach (object key in sqlParas.Keys)
                {
                    sqlCmd.Parameters.Add(new SqlParameter(key.ToString(), sqlParas[key]));
                }
                sqlDr = sqlCmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (sqlDr != null)
                {
                    sqlDr.Close();
                    sqlDr.Dispose();
                }
            }
            finally
            {
            }
            return sqlDr;
        }
        
        /// <summary>
        /// 添加流程步骤
        /// </summary>
        /// <param name="nfs"></param>
        /// <returns></returns>
        public bool AddFlowStep(Flow_NewFlowStep nfs)
        {
            bool flag = false;
            SqlConnection conn = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                conn.Open();
                sc.CommandText = "insert Flow_NewFlowStep(New_Flow_Id,Sort_Id,Prc_Name,Prc_Inputs,Prc_Users,Prc_Sections,Prc_Roles,Next_Prcs_Id) values(@New_Flow_Id,@Sort_Id,@Prc_Name,'','','','',@Next_Prcs_Id)";
                sc.Parameters.Add(new SqlParameter("@New_Flow_Id", nfs.Base_newFlow.NewFlowl_id));
                sc.Parameters.Add(new SqlParameter("@Sort_Id", nfs.Sort_id));
                sc.Parameters.Add(new SqlParameter("@Prc_Name", nfs.Prc_name));
                sc.Parameters.Add(new SqlParameter("@Next_Prcs_Id", nfs.NextPrc_id));
                int i = sc.ExecuteNonQuery();
                if (i > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return flag;
        }
        /// <summary>
        /// 根据流程获取流程步骤信息
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public List<Flow_NewFlowStep> FindFlowStepByFlowId(int fid)
        {
            List<Flow_NewFlowStep> list = new List<Flow_NewFlowStep>();
            SqlConnection conn = null;
            SqlDataReader dr = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                sc.CommandText = "select * from Flow_NewFlowStep where New_Flow_Id=" + fid.ToString() + " order by Sort_Id";
                conn.Open();
                dr = sc.ExecuteReader();
                while (dr.Read())
                {
                    Flow_NewFlowStep f = new Flow_NewFlowStep();
                    f.NewFlowStep_id=(int)dr["Id"];
                    f.NextPrc_id = (string)dr["Next_Prcs_Id"];
                    f.Prc_inputs = (string)dr["Prc_Inputs"];
                    f.Prc_name = (string)dr["Prc_Name"];
                    f.Prc_roles = (string)dr["Prc_Roles"];
                    f.Prc_sections = (string)dr["Prc_Sections"];
                    f.Prc_users = (string)dr["Prc_Users"];
                    f.Sort_id = (int)dr["Sort_Id"];
                    Flow_NewFlow nf = new Flow_NewFlow();
                    nf.NewFlowl_id = (int)dr["New_Flow_Id"];
                    f.Base_newFlow = nf;
                    list.Add(f);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (dr != null)
                {
                    if (!dr.IsClosed)
                    {
                        dr.Close();
                        dr.Dispose();
                    }
                }
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// 根据流程ID获取流程步骤排序号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int FindFlowStepSortByFlowId(int id)
        {
            int i = 0;
            SqlConnection conn = null;
            //SqlDataReader dr = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                sc.CommandText = "select Sort_Id from Flow_NewFlowStep where New_Flow_Id=" + id.ToString() + " order by Sort_Id desc";
                conn.Open();
                Object o = sc.ExecuteScalar();
                if (o != null)
                {
                    i = Convert.ToInt32(o);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return i;
        }
        /// <summary>
        /// 根据流程步骤ID获取流程步骤信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Flow_NewFlowStep FindFlowStepById(int id)
        {
            Flow_NewFlowStep f = new Flow_NewFlowStep();
            SqlConnection conn = null;
            SqlDataReader dr = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                sc.CommandText = "select * from Flow_NewFlowStep where Id=" + id.ToString();
                conn.Open();
                dr = sc.ExecuteReader();
                if (dr.Read())
                {
                    f.NewFlowStep_id = (int)dr["Id"];
                    f.NextPrc_id = (string)dr["Next_Prcs_Id"];
                    f.Prc_inputs = (string)dr["Prc_Inputs"];
                    f.Prc_name = (string)dr["Prc_Name"];
                    f.Prc_roles = (string)dr["Prc_Roles"];
                    f.Prc_sections = (string)dr["Prc_Sections"];
                    f.Prc_users = (string)dr["Prc_Users"];
                    f.Sort_id = (int)dr["Sort_Id"];
                    Flow_NewFlow nf = new Flow_NewFlow();
                    nf.NewFlowl_id = (int)dr["New_Flow_Id"];
                    f.Base_newFlow = nf;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (dr != null)
                {
                    if (!dr.IsClosed)
                    {
                        dr.Close();
                        dr.Dispose();
                    }
                }
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return f;
        }
        /// <summary>
        /// 修改流程步骤基本属性
        /// </summary>
        /// <param name="nfs"></param>
        /// <returns></returns>
        public bool UpdateFlowStepInfo(Flow_NewFlowStep nfs)
        {
            bool flag = false;
            SqlConnection conn = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                conn.Open();
                sc.CommandText = "update Flow_NewFlowStep set Sort_Id=@Sort_Id,Prc_Name=@Prc_Name,Next_Prcs_Id=@Next_Prcs_Id where Id=@Id";
                sc.Parameters.Add(new SqlParameter("@Sort_Id", nfs.Sort_id));
                sc.Parameters.Add(new SqlParameter("@Prc_Name", nfs.Prc_name));
                sc.Parameters.Add(new SqlParameter("@Next_Prcs_Id", nfs.NextPrc_id));
                sc.Parameters.Add(new SqlParameter("@Id", nfs.NewFlowStep_id));
                int i = sc.ExecuteNonQuery();
                if (i > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return flag;
        }
        /// <summary>
        /// 修改流程步骤监控人员
        /// </summary>
        /// <param name="nfs"></param>
        /// <returns></returns>
        public bool UpdateFlowStepManager(Flow_NewFlowStep nfs)
        {
            bool flag = false;
            SqlConnection conn = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                conn.Open();
                sc.CommandText = "update Flow_NewFlowStep set Prc_Users=@Prc_Users where Id=@Id";
                sc.Parameters.Add(new SqlParameter("@Prc_Users", nfs.Prc_users));
                sc.Parameters.Add(new SqlParameter("@Id", nfs.NewFlowStep_id));
                int i = sc.ExecuteNonQuery();
                if (i > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return flag;
        }
        /// <summary>
        /// 修改流程步骤经办部门
        /// </summary>
        /// <param name="nfs"></param>
        /// <returns></returns>
        public bool UpdateFlowStepSections(Flow_NewFlowStep nfs)
        {
            bool flag = false;
            SqlConnection conn = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                conn.Open();
                sc.CommandText = "update Flow_NewFlowStep set Prc_Sections=@Prc_Sections where Id=@Id";
                sc.Parameters.Add(new SqlParameter("@Prc_Sections", nfs.Prc_sections));
                sc.Parameters.Add(new SqlParameter("@Id", nfs.NewFlowStep_id));
                int i = sc.ExecuteNonQuery();
                if (i > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return flag;
        }
        /// <summary>
        /// 修改流程步骤经办角色
        /// </summary>
        /// <param name="nfs"></param>
        /// <returns></returns>
        public bool UpdateFlowStepRoles(Flow_NewFlowStep nfs)
        {
            bool flag = false;
            SqlConnection conn = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                conn.Open();
                sc.CommandText = "update Flow_NewFlowStep set Prc_Roles=@Prc_Roles where Id=@Id";
                sc.Parameters.Add(new SqlParameter("@Prc_Roles", nfs.Prc_roles));
                sc.Parameters.Add(new SqlParameter("@Id", nfs.NewFlowStep_id));
                int i = sc.ExecuteNonQuery();
                if (i > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return flag;
        }
        /// <summary>
        /// 修改流程步骤可写字段
        /// </summary>
        /// <param name="nfs"></param>
        /// <returns></returns>
        public bool UpdateFlowStepInputs(Flow_NewFlowStep nfs)
        {
            bool flag = false;
            SqlConnection conn = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                conn.Open();
                sc.CommandText = "update Flow_NewFlowStep set Prc_Inputs=@Prc_Inputs where Id=@Id";
                sc.Parameters.Add(new SqlParameter("@Prc_Inputs", nfs.Prc_inputs));
                sc.Parameters.Add(new SqlParameter("@Id", nfs.NewFlowStep_id));
                int i = sc.ExecuteNonQuery();
                if (i > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return flag;
        }
        /// <summary>
        /// 根据流程步骤ID删除流程步骤
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelFlowStep(int id)
        {
            bool flag = false;
            SqlConnection conn = null;
            try
            {
                conn = SQLManager.GetConn();
                SqlCommand sc = new SqlCommand();
                sc.Connection = conn;
                conn.Open();
                sc.CommandText = "delete from Flow_NewFlowStep where Id="+id.ToString();
                int i = sc.ExecuteNonQuery();
                if (i > 0)
                {
                    flag = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (conn != null)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
            }
            return flag;
        }
    }
}
